<?php
namespace Home\Controller;
use Think\Controller;
class ShopController extends Controller
{
    protected $wkqData;

    public function _initialize()
    {
        $this->pubHead();

        $nav = M('classgood')->where('pid = 0 and  navgroom =  "1"')->order('sort desc')->limit(8)->select();
        $this->assign('nav',$nav);

        import('ORG.Util.Pageno');

        //为你推荐
        $tjList=M('commodity')->limit(10)->order('rand()')->select();
        $this->assign('tjList',$tjList);
    }

    //商城首页
    public function index()
    {
        //获取首页栏目列表
        $nav = D('classgood')->findNav();
        $this->assign('nav',$nav);

        //获取首页广告图1
        $advImgs = D('advert')->findAdvImg();
        $this->assign('advImgs',$advImgs);

        //热品推荐商品列表
        $hotTjPro=M('commodity')->where('fkqg=1')->select();
        $this->assign('hotTjPro',$hotTjPro);

        //获取温控器下面的数据
        $wkqData = D('shop')->showFirstpage(84,'', 'items');
        $this->assign('wkqData',$wkqData);
        //dump($wkqData);

        //获取温控配件下面的数据
        $wkpjData = D('shop')->showFirstpage(87,'', 'items');
        $this->assign('wkpjData',$wkpjData);
        //dump($wkpjData);

        //获取数码配件下面的数据
        $smpjData = D('shop')->showFirstpage(91,'', 'items');
        $this->assign('smpjData',$smpjData);
        //dump($smpjData);

        $this->display();
    }

    //分类产品列表页
    public function shopItemList(){

        /*$nav = M('classgood')->where('pid = 0 and  navgroom =  "1"')->order('sort desc')->limit(8)->select();
        $this->assign('nav',$nav);*/

        $pid=I('get.pid');
        $list = D('shop')->showFirstpage($pid,'', 'erji');
        //dump($list);
        $this->assign('list',$list);

        /*//为你推荐
        $tjList=M('commodity')->limit(10)->order('rand()')->select();
        $this->assign('tjList',$tjList);*/

        $this->display();
    }

   //商品详情页
    public function article(){

        $id=I('get.id');
        $product = D('shop')->getProInfoById($id);
        //dump($product);
        $this->assign('coupon',$product['coupon']);
        $this->assign('picList',$product['picList']);
        $this->assign('proInfo',$product['proInfo']);

        //常见问题
        $wtList=M('content')->where('cid = 64')->select();
        $this->assign('wtList',$wtList);

        //猜你喜欢
        $tjList=M('commodity')->limit(4)->order('rand()')->select();
        $this->assign('tjList',$tjList);

            //获取当前商品的规格参数
        $proNotice=M('forpar')->where('pid = "'.$id.'"')->select(); //dump($proNotice);
        for ($i=0;$i<count($proNotice);$i++) {
            $class_name = M('class')->where('id = "'.$proNotice[$i]['pcateid'].'"')->find(); //dump($class_name);
            $proNotice[$i]['class_name'] = $class_name['class_name'];
        }

        $this->assign('proNotice',$proNotice);
        /*$stock = M('stock')->where('goods_id = "'.$id.'"')->select();
        $this->stock = $stock;*/
        $this->detail = 1;
        $this->show = 1;
        $this->display();
    }

    //列表页
    public function  shopList(){
        //import('Org.Util.Pageno');

        $M = M("commodity");
        if($_GET['id']){
            $class_name = M('classgood')->where('id =  "'.$_GET['id'].'"')->find();
            $this->assign('class_name',$class_name);
            //$where['pid']=$class_name['id'];
            $where['pid']=$_GET['id'];
            $count=$M->where($where)->count();
            $page = new \Page($count,10);
            $limit = $page->firstRow . ',' . $page->listRows;
            $proList=$M->where($where)->limit($limit)->order('createtime desc')->select();
            $showPage = $page->show();

        }else{
            $count=$M->count(); //dump($M); dump($count);
            $page=new \Page($count,10);
            $showPage = $page->show();
            $proList=$M->order('createtime desc')->limit("$page->firstRow, $page->listRows")->select();

        }
        $this->assign("page", $showPage);
        $this->assign("list",$proList);
        /*//为你推荐
        $tjList=M('commodity')->limit(10)->order('rand()')->select();
        $this->assign('tjList',$tjList);*/

        $this->display();
    }

     //搜索产品列表页
    public function search(){

        //产品分类

        $goodName=$_GET['goodName'];
        //搜索商品
        $count = M('commodity')->where('title like  "%'.$goodName.'%"')->count();
        $page = new \Page($count,1);
        $limit = $page->firstRow . ',' . $page->listRows;
        $list = M('commodity')->where('title like  "%'.$goodName.'%"')->limit($limit)->order('createtime desc')->select();
        $show = $page->show();
        dump($show);
        $this->assign('page',$show);
        $this->assign('list',$list);

        $this->display();
    }





     //登录页
    public function login(){
        if($_POST){
            $user = I('post.user');
            $password =  md5(I('post.password'));
            $re = M('personal')->where('user = "'.$user.'" and pwd = "'.$password.'"')->find();
            if(!$re){
                exit('<script>alert("登录失败!");location.href="/Home/Shop/login.html";</script>');
            }else{
                session('id',$re['id']);  //ID
                session('name',$re['user']);   //用户名
                session('type','personal');   //类别
                session('state',1);   //状态
                session('vip',$re['vip']); //vip
                session('userInfo',$re);
                if(isset($_POST['checkInfo'])){
                    setcookie(session_name(),session_id(),time()+7*24*3600,'/');
                }else{
                    setcookie(session_name(),session_id(),0,'/');
                }
                exit('<script>alert("登录成功!");location.href="/Home/Shop/shop_cart.html";</script>');
            }
        }
        //网站信息
        $info=M('info')->find();
        $this->assign('info',$info);
        $this->display();
    }






    //ajax获取首页点击栏目时的对应数据
    public function getCateData(){

        $cateId=I('post.pid');
        $cateData=M('commodity')->where('pid ="'.$cateId.'"')->limit(8)->select();
        echo json_encode(array("data"=>$cateData,"status"=>1));
    }

    //获取公共数据
    public function pubHead()
    {
        //顶部优惠券广告图
        $couAdv=M('classgood')->where('id=98')->find();
        $this->assign('couAdv',$couAdv);
        //dump($couAdv);
        //banner管理
        $banner = M('image')->where('cid = 0 and recommend = 1')->order('sort desc')->select();
        $this->assign('banner',$banner);
        //底部服务列表
        $footSerList=M('class')->where('pid=61')->select();
        $this->assign('footSerList',$footSerList);
        ///网站信息
        $info=M('info')->find();
        $this->assign('info',$info);

    }



}